/**
 * Created With IntelliJ IDEA
 * Description:牛客网：BM32 合并二叉树
 * <a href="https://www.nowcoder.com/practice/7298353c24cc42e3bd5f0e0bd3d1d759?tpId=295&tqId=1025038&ru=/exam/oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2Foj">...</a>
 * User: DELL
 * Data: 2022-12-22
 * Time: 22:38
 */
import java.util.*;


class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}


public class Solution {
    public TreeNode mergeTrees (TreeNode t1, TreeNode t2) {
        //递归终止条件,即两结点至少有一个为null时
        if (t1 == null) {
            return t2;
        }
        if (t2 == null) {
            return t1;
        }
        //当两结点均不为null时
        TreeNode head = new TreeNode(t1.val + t2.val);
        head.left = mergeTrees(t1.left,t2.left);
        head.right = mergeTrees(t1.right,t2.right);
        return head;
    }
}